const {
    version
} = require("./constants.js")
const programe = require("commander")
const path = require("path")
// 配置3个指令命令
const mapActions = {
    create: {
        alias: "c",
        description: "create a project",
        examples: ["tsc_cli create <project-name>"]
    },
    config: {
        alias: "conf",
        description: "config project vairable",
        examples: ["tsc_cli config set <k><v>", "tsc_cli config get <k>"],
    },
    "*": {
        alias: "",
        description: "command not found",
        examples: [],
    }
}

// 循环创建命令
Reflect.ownKeys(mapActions).forEach(action => {
    programe.command(action) //配置命令的名称
        .alias(mapActions[action].alias) //命令别名
        .description(mapActions[action].description) //描述
        .action(() => {
            if (action == "*") {
                console.log(mapActions[action].description);
            } else {
                // 截取命令
                require(path.resolve(__dirname, action))(...process.argv.slice(3));
            }
        })
})

// 监听用户的help事件
programe.on("--help", () => {
    console.log("\nExamples:");
    Reflect.ownKeys(mapActions).forEach((action) => {
        mapActions[action].examples.forEach((example) => {
            console.log(example);
        })
    })
})

// 获取版本号
programe.version(version).parse(process.argv)